package com.kun.admin.authority.service;

import com.kun.admin.authority.dao.RoleDao;
import com.kun.admin.authority.model.MenuItem;
import com.kun.admin.authority.model.RoleInfo;
import com.kun.admin.authority.model.RoleModel;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by likun on 2017/4/2.
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    /**
     * 查询所有的角色
     * @return
     */
    @Override
    public List<RoleModel> findAll() {
        return roleDao.findAll();
    }

    /**
     * 添加角色
     * @param roleModel
     * @return
     */
    @Override
    public boolean create(RoleModel roleModel) {
        int i=roleDao.create(roleModel);
        return i>0;
    }

    /**
     * 根据id获取角色
     * @param id
     * @return
     */
    @Override
    public RoleModel getRoleById(Long id) {
        return roleDao.getRoleById(id);
    }

    /**
     * 修改角色信息
     * @param roleModel
     * @return
     */
    @Override
    public boolean update(RoleModel roleModel) {
        int i=roleDao.update(roleModel);
        return i>0;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        int i=roleDao.delete(id);
        return i>0;
    }

    /**
     * 查询角色的所有菜单权限
     * @param id
     * @return
     */
    @Override
    public ArrayList<MenuItem> findMenus(Long id) {
        return roleDao.findMenus(id);
    }

    /**
     *
     * @param menuId
     * @param roleId
     * @return
     */
    @Override
    public boolean addRoleMenu(Long menuId, Long roleId) {
        int i=roleDao.addRoleMenu(roleId,menuId);
        return i>0;
    }

    /**
     * 移除角色的权限
     * @param menuId
     * @param roleId
     * @return
     */
    @Override
    public boolean deleteRoleMenu(Long menuId, Long roleId) {
        if (menuId == null && roleId == null) {
            return false;
        }
        int i=roleDao.deleteRoleMenu(menuId,roleId);
        return i>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addRole(RoleInfo roleInfo) {
        roleDao.create(roleInfo);
        if (CollUtil.isNotEmpty(roleInfo.getMenuKeys())){
            for (Long menuId : roleInfo.getMenuKeys()) {
                boolean b=addRoleMenu(menuId,roleInfo.getId());
                if (!b || roleInfo.getId()==null){
                    throw new RuntimeException("插入角色失败");
                }
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRole(RoleInfo roleInfo) {
        if (roleInfo.getId()==null){
            return false;
        }
        update(roleInfo);
        //先删除角色当前所拥有的所有菜单权限
        deleteRoleMenu(null,roleInfo.getId());
        if (CollUtil.isNotEmpty(roleInfo.getMenuKeys())){
            for (Long menuId : roleInfo.getMenuKeys()) {
                boolean b=addRoleMenu(menuId,roleInfo.getId());
                if (!b){
                    throw new RuntimeException("角色权限更新失败");
                }
            }
        }
        return true;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRole(Long roleId) {
        boolean delete = delete(roleId);
        if (!delete){
            throw new RuntimeException("删除角色失败");
        }
        deleteRoleMenu(null,roleId);
        return true;
    }
}
